package com.efast.dmom.mgmt.service.mes.report.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.efast.cafe.commponent.query.service.impl.QueryServiceImpl;
import com.efast.cafe.framework.bean.QueryPageBase;
import com.efast.cafe.framework.jdbc.JdbcDao;
import com.efast.cafe.portal.bean.query.QueryBean;
import com.efast.cafe.portal.bean.query.QueryConditionBean;
import com.efast.cafe.portal.bean.query.QueryOpBean;
import com.efast.dmom.mgmt.dao.mes.report.ReportVehicleRetrospectDao;
import com.efast.dmom.mgmt.entity.report.ReportVehicleRetrospect;

@Service("qm_bas_defectrepair_record_QT533-1017")
public class ReportRepairInfoServiceImpl  extends QueryServiceImpl {
@Autowired
private ReportVehicleRetrospectDao reportVehicleRetrospectDao;
@Autowired
@Qualifier("jdbcDao")
private JdbcDao jdbcDao;

@Override
public Object queryList(QueryBean querys) {
	QueryPageBase result = new QueryPageBase();
	QueryConditionBean queryConditionBean = querys.getQueryConditionBean();
	String sql = "";
	String sql2 ="SELECT\r\n" + 
			"	re.id AS id,\r\n" + 
			"	re.repairer AS repairer,\r\n" + 
			"	re.repair_code AS repairCode,\r\n" + 
			"	co.comments as repairCodeDesc,\r\n" + 
			"	re.comments AS comments,\r\n" + 
			"	date_format(re.create_time, '%Y-%m-%d %H:%i:%s' )  AS createTime,\r\n" + 
			"	re.create_user AS createUser,\r\n" + 
			"	date_format(re.last_update_time, '%Y-%m-%d %H:%i:%s') AS lastUpdateTime,\r\n" + 
			"	re.last_update_user AS lastUpdateUser,\r\n" + 
			"	re.is_deleted AS isDeleted,\r\n" + 
			"	re.repair_code_group AS repairCodeGroup,\r\n" + 
			"	gr.comments AS repairCodeGroupDesc,\r\n" + 
			"	lot.vin AS vin,\r\n" + 
			"	lot.self_number AS selfNumber,\r\n" + 
			"	re.is_key_part AS isKeyPart,\r\n" + 
			"	re.bom_item_name AS bomItemName,\r\n" + 
			"	re.consumed_part_sn AS consumedPartSn,\r\n" + 
			"	re.replaced_consumed_part_sn AS replacedConsumedPartSn,\r\n" + 
			"	re.replace_part_no_version AS replacePartNoVersion,\r\n" + 
			"	re.replace_number AS replaceNumber,\r\n" + 
			"	re.part AS part,\r\n" + 
			"	part.part_no_version AS partNoVersion,\r\n" + 
			"	part.part_name_c AS partName,\r\n" + 
			"	re.station AS stationName  \r\n" + 
			"  FROM qm_bas_defectrepair_record AS re\r\n" + 
			"	LEFT JOIN tc_bas_repair_group as gr on re.repair_code_group = gr.repair_group_name\r\n" + 
			"	LEFT JOIN tc_bas_repair_code as co on re.repair_code = co.repair_code\r\n" + 
			"	LEFT JOIN tm_bas_part AS part ON part.part_no = re.part\r\n" + 
			"	LEFT JOIN tw_bas_lot AS lot ON re.lot_name = lot.lot_name where  lot.lot_name not like '%-A' ";
	String selfLotName =""; 
	String vinLotName = "";
	String selfNumber = "";
	Integer start = 0;
	Integer length = 0;
	if(queryConditionBean.getStart() == null || queryConditionBean.getLength() == null) {
		start = 0;
		length =querys.getTemplate().getPagenation();
	}
    start = queryConditionBean.getStart();
	length = queryConditionBean.getLength();
	List<QueryOpBean> list = queryConditionBean.getQueryOpBeans();
	for(int i=0;i<list.size();i++) {
		String key = list.get(i).getKey().trim();
		String value = list.get(i).getValue().trim();
		if(key!=null &&!"".equals(key)&& value!=null && !"".equals(value) ) {
			if(key.equals("lot.vin")) {
				//获取车辆合拼信息
				List<ReportVehicleRetrospect> spliceInfo = reportVehicleRetrospectDao.getSpliceInfo(value);
				if(spliceInfo!=null && spliceInfo.size()>0) {
					for (int j=0;j<spliceInfo.size();j++) {
						
						//说明为货车合拼之后的查询
						 vinLotName = spliceInfo.get(j).getLotName();						
						 selfNumber = spliceInfo.get(j).getSelfNumber();
						 List<ReportVehicleRetrospect> selfLotNameList = reportVehicleRetrospectDao.getSelfLotName(selfNumber);
						 if(selfLotNameList!=null &&selfLotNameList.size()>0) {
							 selfLotName= selfLotNameList.get(0).getLotName();
						 }
						 if(j==0) {
							 sql+="  re.lot_name like '%"+vinLotName+"%' or re.lot_name like '%"+selfLotName+"%' ";
						 }else {
							 sql+=" or re.lot_name like '%"+vinLotName+"%' or re.lot_name like '%"+selfLotName+"%' ";
						 }
						 
					}
					sql="and ("+sql+"or lot.vin like '%"+value+"%' or lot.self_number like '%"+value+"%' )";
				}else {
					sql+="and (lot.vin like '%"+value+"%' or lot.self_number like '%"+value+"%' )";
				}
				
			}else if(key.equals("re.create_time")) {
				if(value!=null &&value.length()>0) {
					String[] split = value.split("~");
					if(split!=null &&split.length>1) {
						String beginTime = split[0];
						String endTime = split[1];
						sql+="and "+key+" between '"+beginTime+"' and '"+endTime+"'";
					}
					
				}
				
			}else {
				sql+="and "+key+" like '%"+value+"%'";
			}
		}
	}
	sql = sql2+sql;
	int count = jdbcDao.queryCount(sql);
	sql+=" GROUP BY re.id";
	sql+=" order by re.create_time DESC";
	if(length!=null && length!=0) {
		sql+=" limit "+start+","+length;	
	}		
	List<Map<String, Object>> results = jdbcDao.queryForListMap(sql);
	result.setResultList(results);
	result.setTotalRows(count);
	return result;
	
}
}
